package com.tqw.shipping.controller;

import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.entity.StatusCode;
import com.tqw.exception.TqwExcepition;
import com.tqw.shipping.dto.ShippingDTO;
import com.tqw.shipping.pojo.Receiving;
import com.tqw.shipping.service.ShippingService;
import com.tqw.vo.ShippingVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: czm
 * @Date: 2022/04/12/19:29
 * @Description:
 */
@RestController
@RequestMapping("/shipping")
public class ShippingController {

    @Autowired
    private ShippingService shippingService;

    @Autowired
    private MapperFacade mapperFacade;

    private static final Integer MAX_USER_ADDR = 5;

    /**
     * 获取收货地址列表
     * @param token
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShippingDTO>> list(@RequestHeader("Authorization") String token){
        Integer clientId = JWTUtil.getId(token);
        Result<List<ShippingDTO>> list = shippingService.list(clientId);
        return list;
    }

    /**
     * 获取单个收货地址信息
     * @param token
     * @param addressId
     * @return
     */
    @GetMapping("/oneAddr")
    public Result<ShippingVO> getAddrId(@RequestHeader("Authorization") String token,@RequestParam("addressId") Integer addressId){
        Integer clientId = JWTUtil.getId(token);
        System.out.println("-------------");
        System.out.println(clientId);
        ShippingDTO addrinfo = shippingService.getAddrByClientIdAndId(addressId,clientId);
        ShippingVO shippingVO = mapperFacade.map(addrinfo, ShippingVO.class);
        Result<ShippingVO> voResult = new Result<>();
        voResult.setData(shippingVO);
        return voResult;
    }

    /**
     * 新增收货地址信息
     * @param token
     * @param shippingVO
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestHeader("Authorization") String token,@RequestBody ShippingVO shippingVO){
        Integer clientId = JWTUtil.getId(token);
        int shippingCount = shippingService.countByUserId(clientId);
        System.out.println(shippingCount);
        if (shippingCount >= MAX_USER_ADDR){
            return new Result(false, StatusCode.ERROR,"收货地址已达上限,无法再新增地址");
        }
        Receiving receiving = mapperFacade.map(shippingVO, Receiving.class);
        if (shippingCount == 0){
            receiving.setIsDefault(Receiving.DEFAULT_ADDR);
        }else if (!Receiving.DEFAULT_ADDR.equals(receiving.getIsDefault())){
            receiving.setIsDefault(Receiving.NOT_DEFAULT_ADDR);
        }
        receiving.setAddressId(null);
        receiving.setClientId(clientId);
        receiving.setCreateBy(clientId).setUpdateBy(clientId).setCreatedTime(new Date()).setUpdatedTime(new Date());
        shippingService.save(receiving);
        return new Result();
    }

    /**
     * 更新收货地址/包括更新默认收货地址
     * @param token
     * @param shippingVO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestHeader("Authorization") String token,@RequestBody ShippingVO shippingVO){
        Integer clientId = JWTUtil.getId(token);
        ShippingDTO addr = shippingService.getAddrByClientIdAndId(shippingVO.getAddressId(), clientId);
        System.out.println(addr);
        if (addr == null){
            throw new TqwExcepition("地址已被删除");
        }else if (addr.getIsDefault().equals(Receiving.DEFAULT_ADDR) && shippingVO.getIsDefault()
        .equals(Receiving.NOT_DEFAULT_ADDR)){
            throw new TqwExcepition("默认地址不可更改为普通地址");
        }
        Receiving updateAddr = mapperFacade.map(shippingVO, Receiving.class);
        updateAddr.setClientId(clientId).setUpdateBy(clientId).setUpdatedTime(new Date());
        shippingService.update(updateAddr);
        return new Result();
    }

    /**
     * 删除收货地址
     * @param token
     * @param addressId
     * @return
     */
    @DeleteMapping("/delete/{addressId}")
    public Result delete(@RequestHeader("Authorization") String token,@PathVariable Integer addressId){
        Integer clientId = JWTUtil.getId(token);
        ShippingDTO addr = shippingService.getAddrByClientIdAndId(addressId, clientId);
        if (addr == null){
            throw new TqwExcepition("该地址已被删除");
        }else if (addr.getIsDefault().equals(Receiving.DEFAULT_ADDR)){
            return new Result(false,StatusCode.ISDEFAULY,"默认地址不可删除");
//            throw new TqwExcepition("默认地址不能删除");
        }
        shippingService.deleteAddrByClientIdAndId(addressId,clientId);
        return new Result();
    }

}
